package com.bawei.module_motocircle.helper

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.bawei.lib_common.base.adapter.CommonVDBRecyclerAdapter
import com.bawei.lib_common.dp
import com.bawei.module_motocircle.R
import com.bawei.module_motocircle.databinding.ItemPopupMenuRowBinding
import com.bawei.module_motocircle.databinding.LayoutPopupNavigationBinding
import com.bawei.module_motocircle.model.ModelItem
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @author yaotianxue
 * @description 二级弹窗
 * @date 2025/5/29
 */
class SeriesPopupMenuRvHelper(
    private val layoutInflater: LayoutInflater,
    private val onSelect: ((String, ModelItem) -> Unit)? //选中二级目录回调
) {
    private var mAdapter: CommonVDBRecyclerAdapter<ModelItem, ItemPopupMenuRowBinding>? = null
    private var mItems = ArrayList<ModelItem>()

    private var mNavigationId: String = "" //一级目录id

    /**
     * 绑定数据
     */
    fun bindItems(id: String, list: List<ModelItem>) {
        this.mNavigationId = id
        mItems.clear()
        mItems.addAll(list)
    }

    private val mBinding by lazy {
        LayoutPopupNavigationBinding.inflate(layoutInflater)
    }
    private val mPopupWindow by lazy {
        PopupWindow().apply {
            width = ViewGroup.LayoutParams.WRAP_CONTENT
            height = ViewGroup.LayoutParams.WRAP_CONTENT
            isFocusable = true
            isOutsideTouchable = true
            contentView = mBinding.root
        }
    }
    private fun showAsDropDown(v: View) {
        mPopupWindow.showAsDropDown(v, -(50.dp - v.width / 2), 10.dp)
    }
    fun showAgain(view: View) {
        this.showAsDropDown(view)
    }

    fun show(view: View): SeriesPopupMenuRvHelper {
        this.showAsDropDown(view)
        initAdapter()
        return this
    }

    private fun initAdapter() {
        if (mAdapter == null){
            mAdapter = object :CommonVDBRecyclerAdapter<ModelItem, ItemPopupMenuRowBinding>(R.layout.item_popup_menu_row, mItems){
                override fun convert(
                    holder: BaseDataBindingHolder<ItemPopupMenuRowBinding>,
                    item: ModelItem
                ) {
                    holder.dataBinding?.item = item
                    holder.dataBinding?.tvMenu?.isSelected = item.checked //选中状态
                    holder.dataBinding?.executePendingBindings() //刷新UI

                }

            }
            mBinding.rvPopupMenu.adapter = mAdapter
            mAdapter!!.setOnItemClickListener { adapter, _, position ->
                mItems
                val target = mItems[position]
                target.checked = true
                adapter.notifyDataSetChanged()
                onSelect?.invoke(mNavigationId, target) //一级目录id，二级目录数据回调
                this@SeriesPopupMenuRvHelper.mPopupWindow.dismiss()
            }
        }else{
            mAdapter?.notifyDataSetChanged()
        }

    }
}